/* eslint-disable space-before-blocks */
/* eslint-disable no-use-before-define */
import { message } from 'antd'
import { jsPDF } from "jspdf"
import html2canvas from 'html2canvas'

export function debounce(fn, delay=200) {
  let timer;
  return function () {
    let th = this;
    let args = arguments;
    if (timer) {
      clearTimeout(timer);
    }
    timer = setTimeout(function () {
      timer = null;
      fn.apply(th, args);
    }, delay);
  };
}

export function throttle(fn, delay) {
  let last;
  let timer;
  let interval = delay || 200;
  return function () {
    let th = this;
    let args = arguments;
    let now = +new Date();
    if (last && now - last < interval) {
      clearTimeout(timer);
      timer = setTimeout(function () {
        last = now;
        fn.apply(th, args);
      }, interval);
    } else {
      last = now;
      fn.apply(th, args);
    }
  }
}
export async function valiFunc(arr) {
  let res = true
  for (let item of arr) {
    if (!item.data || item.data.length === 0) {
      res = false
      message.error(
        item.msg
      )
      break
    }
  }
  return res
}

export function bottomHandle(isScroll, callback, status) {  // 触底加载  callback 是传过来获取列表的
  let index = 0
  const throttleScroll = throttle(scrollChange.bind(null, isScroll, callback), 200)
  console.log('====================================1');
  return function(){
    console.log('====================================zx',index);

    if(index>0){
      window.removeEventListener('scroll', throttleScroll)
    }else{
      window.addEventListener('scroll', throttleScroll)
    }
    index ++

  }

  // window.onscroll = throttle(scrollChange.bind(null, isScroll, callback), 200)

}

function scrollChange(isScroll, callback) {
 
  if (!isScroll) return
  const scrollTop = Math.ceil(document.documentElement.scrollTop || document.body.scrollTop || window.pageYOffset)
  const windowHeight = document.documentElement.clientHeight || document.body.clientHeight
  const scrollHeight = document.documentElement.scrollHeight || document.body.scrollHeight || window.scrollHeight
  console.log(scrollTop, windowHeight, scrollHeight)
  if (scrollTop + windowHeight >= scrollHeight) {
    // console.log(scrollTop, windowHeight, scrollHeight)
    // console.log("执行回调")
    callback()
  }
}

export function createPdf(id) {
  var target = document.getElementById(id);
  console.dir(target)
  let pageData =''
  // target.style.background = "#FFFFFF";
   console.log(html2canvas)
  html2canvas(target,{
    useCORS:true,
    width:window.screenX,
    height:window.screen.availHeight,
    windowHeight:target.scrollHeight,
    windowWidth:target.scrollWidth,
    x:0,
    y:window.pageYOffset

  }).then((canvas)=>{
    console.log('准备执行')
    var contentWidth = canvas.width;
    var contentHeight = canvas.height;
    //一页pdf显示html页面生成的canvas高度;
    var pageHeight = contentWidth / 592.28 * 841.89;
    //未生成pdf的html页面高度
    var leftHeight = contentHeight;
    //页面偏移
    var position = 0;
    //a4纸的尺寸[595.28,841.89]，html页面生成的canvas在pdf中图片的宽高
    var imgWidth = 595.28;
    var imgHeight = 592.28/contentWidth * contentHeight;

     pageData = canvas.toDataURL('image/jpeg', 1.0);
     console.log(pageData,'base64')

    var pdf = new jsPDF('', 'pt', 'a4');
    console.log('pdf执行了吗')
    //有两个高度需要区分，一个是html页面的实际高度，和生成pdf的页面高度(841.89)
    //当内容未超过pdf一页显示的范围，无需分页
    if (leftHeight < pageHeight) {
    pdf.addImage(pageData, 'JPEG', 0, 0, imgWidth, imgHeight );
    } else {
        while(leftHeight > 0) {
            pdf.addImage(pageData, 'JPEG', 0, position, imgWidth, imgHeight)
            leftHeight -= pageHeight;
            position -= 841.89;
            //避免添加空白页
            if(leftHeight > 0) {
              pdf.addPage();
            }
        }
    }
    
    pdf.save("content.pdf");
  })
 
}